#region Copyright Notice
// 
// 
//  
//  Copyright (c) 2009-2011 Anindya Chatterjee
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// 
// 
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace AbstractClass.IO
{
    /// <summary>
    /// Represents a mechanism to iterate through the lines in a file.
    /// </summary>
    public class FileLineIterator : IEnumerable<string>, IDisposable
    {
        private readonly FileStream _fStream;
        private bool _disposed;

        /// <summary>
        /// Initializes a new instance of the <see cref="FileLineIterator"/> class.
        /// </summary>
        /// <param name="fileStream">The file stream containing the lines to iterate.</param>
        public FileLineIterator(FileStream fileStream)
        {
            _fStream = fileStream;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FileLineIterator"/> class.
        /// </summary>
        /// <param name="fileName">Name of the file to iterate.</param>
        /// <param name="fileMode">A constant that determines how to open or create the file.</param>
        public FileLineIterator(string fileName, FileMode fileMode)
            : this(new FileStream(fileName, fileMode))
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FileLineIterator"/> class.
        /// </summary>
        /// <param name="filePath">The path of the file to iterate.</param>
        public FileLineIterator(string filePath)
            :this(new FileStream(filePath, FileMode.Open))
        {
        }


        #region Implementation of IEnumerable

        /// <summary>
        /// Returns an enumerator that iterates through the lines in the file.
        /// </summary>
        /// <returns>A <see cref="IEnumerator&lt;String&gt;"/> implementation for the file lines.</returns>
        /// <exception cref="NotSupportedException">File stream is not readable or seek able.</exception>
        public IEnumerator<string> GetEnumerator()
        {
            _fStream.Seek(0, SeekOrigin.Begin);
            if (!_fStream.CanSeek)
            {
                throw new NotSupportedException(LanguageResource.SeekIsNotSupportedWithinTheStream);
            }

            if (!_fStream.CanRead)
            {
                throw new NotSupportedException(LanguageResource.ReadIsNotSupportedWithinTheStream);
            }

            var streamReader = new StreamReader(_fStream);

            string line;
            while ((line = streamReader.ReadLine()) != null)
            {
                yield return line;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        #region Implementation of IDisposable

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed) return;
            if (disposing)
            {
                if (_fStream != null) _fStream.Dispose();
            }

            _disposed = true;
        }

        /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// <see cref="FileLineIterator"/> is reclaimed by garbage collection.
        /// </summary>
        ~FileLineIterator()
        {
            Dispose(false);
        }

        #endregion
    }
}